cmake_minimum_required(VERSION 3.29.0)

project(PokerBox VERSION 0.1.0 LANGUAGES C CXX)

include(CTest)
enable_testing()

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")  
include(Functions)

# 使用 get_filename_component 命令来获取这个目录的绝对路径  
get_filename_component(MODULES_DIR modules ABSOLUTE)
get_filename_component(INCLUDE_DIR include ABSOLUTE)
get_filename_component(BUILD_DIR build ABSOLUTE)
get_filename_component(SOURCE_DIR src ABSOLUTE)

# 设置 exe 输出路径
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${BUILD_DIR}/bin)  

# 使用 sdl2 库
set(SDL_MODULE "${MODULES_DIR}/SDL2-devel-2.30.1-mingw/SDL2-2.30.1/i686-w64-mingw32")
set(SDL2_DLL_FILE "${SDL_MODULE}/bin/SDL2.dll")
set(SDL_LIB_DIR "${SDL_MODULE}/lib")
set(SDL_INCLUDE "${SDL_MODULE}/include/SDL2")

# 使用 imgui 库
set(IMGUI_MODULE "${MODULES_DIR}/imgui")
set(IMGUI_CPP_FILES
"${IMGUI_MODULE}/imgui.cpp"
"${IMGUI_MODULE}/imgui_demo.cpp"
"${IMGUI_MODULE}/imgui_draw.cpp"
"${IMGUI_MODULE}/imgui_tables.cpp"
"${IMGUI_MODULE}/imgui_widgets.cpp"
"${IMGUI_MODULE}/backends/imgui_impl_opengl2.cpp"
"${IMGUI_MODULE}/backends/imgui_impl_sdl2.cpp")
set(IMGUI_INCLUDE
"${IMGUI_MODULE}"
"${IMGUI_MODULE}/backends"
"${IMGUI_MODULE}/misc")

# Controls 头文件
set(CONTROLS_INCLUDE
${INCLUDE_DIR}
${INCLUDE_DIR}/nlohman
${INCLUDE_DIR}/Controls
${INCLUDE_DIR}/Models
${INCLUDE_DIR}/Utils)

set(SRC_CPP_FILES
"${SOURCE_DIR}/main.cpp"
"${SOURCE_DIR}/Controls/ContentPresenter.cpp"
"${SOURCE_DIR}/Controls/CheckListDataGrid.cpp")

# 源文件
set(CPP_FILES 
${SRC_CPP_FILES}
${IMGUI_CPP_FILES})

include_directories(${SDL_INCLUDE} ${IMGUI_INCLUDE} ${CONTROLS_INCLUDE})

link_directories(${SDL_LIB_DIR})
add_executable(${PROJECT_NAME} ${CPP_FILES})

# 链接的顺序必须为：mingw32; opengl32; SDL2main; SDL2;
# 否则会报错不能找到 WinMain
target_link_libraries(${PROJECT_NAME} "mingw32;opengl32;SDL2main;SDL2;")

# 这里只要链接到资源文件，就会使用资源文件里的 ID 最低的值的 ICO 文件作为引用程序图标
set(RESOURCE_FILE ${SOURCE_DIR}/Assets/Resource.rc)
target_sources(${PROJECT_NAME} PRIVATE ${RESOURCE_FILE})

copy_directory("${SOURCE_DIR}/Assets" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
copy_file("${SOURCE_DIR}/AppSetting.json" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
copy_file("${SOURCE_DIR}/Hello.txt" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
copy_file("${MODULES_DIR}/curl/libcurl.dll" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
copy_file(${SDL2_DLL_FILE} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})

# 安装 ${PROJECT_NAME} 目标输出文件到打包目录
# ${PROJECT_NAME} 目标是生成 exe 的，故将 exe 拷贝到打包目录
install(TARGETS ${PROJECT_NAME} DESTINATION .)

# 拷贝 DLL 文件到打包目录
set(MODULES_DLL_FILES
${MODULES_DIR}/curl/libcurl.dll
${MODULES_DIR}/SDL2-devel-2.30.1-mingw/SDL2-2.30.1/x86_64-w64-mingw32/bin/SDL2.dll)
install(FILES ${MODULES_DLL_FILES} DESTINATION .)
install(FILES ${SOURCE_DIR}/AppSetting.json DESTINATION .)

# 拷贝资源文件到打包目录
install(DIRECTORY  ${SOURCE_DIR}/Assets DESTINATION .)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
set(CPACK_GENERATOR "ZIP")  
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A gui app for windows.")  
set(CPACK_INSTALL_PREFIX "/") # 设置安装前缀为根目录，这样文件会直接放在包内 
set(CPACK_INCLUDE_TOP_LEVEL_DIRECTORY OFF)  
include(CPack)

